package ph.loong.deliveryman.base

import android.content.Context
import android.graphics.drawable.Drawable
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.View
import android.view.inputmethod.InputMethodManager
import android.widget.EditText
import com.classic.common.MultipleStatusView
import com.qmuiteam.qmui.util.QMUIStatusBarHelper
import deliveryman.loong.ph.deliveryman.R
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.disposables.Disposable
import kotlinx.android.synthetic.main.tool_bar_view.*
import ph.loong.deliveryman.app.MyApplication
import ph.loong.deliveryman.view.StatusBarUtil
import ph.loong.deliveryman.view.progress.DialogWaitting

/**
 * Author jc.lu
 * Dates 2018/2/8.
 * Email Tbsupland@163.com
 */
abstract class BaseActivity : AppCompatActivity() {
    /**
     * 多种状态的 View 的切换
     */
    protected var mLayoutStatusView: MultipleStatusView? = null

    private var compositeDisposable = CompositeDisposable()

    private val showDialog: DialogWaitting by lazy { DialogWaitting(this) }


    fun addSubscription(disposable: Disposable) {
        compositeDisposable.add(disposable)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val application = MyApplication.getAplication()
        application?.addActivity(this)
        initListener()
        QMUIStatusBarHelper.setStatusBarLightMode(this)
        StatusBarUtil.setColorNoTranslucent(this, this.getResources().getColor(R.color.white))
        showDialog?.mDialog.setOnDismissListener {
            //关闭dialog 后取消联网操作
            compositeDisposable.clear()
        }
    }

    private fun initListener() {
        mLayoutStatusView?.setOnClickListener(mRetryClickListener)
    }


    open val mRetryClickListener: View.OnClickListener = View.OnClickListener {
        //        start()
        retryClick()
    }

    /**
     * 重新联网
     */
    open fun retryClick() {

    }


    /**
     * 设置title
     */
    open fun setTitle(title: String) {
        title_text.setText(title)
        image_back?.setOnClickListener({ finish() })
    }

    /**
     * 设置右侧title
     */
    open fun setRightTitle(title: String) {
        right_text.setText(title)
        right_text.setOnClickListener({
            mRightOnClickListener?.onRightImgClick()
        })
    }

    /**
    设置右侧图标
     **/
    open fun setRightImg(drawable: Drawable) {
        right_img.setImageDrawable(drawable)
        right_img.setOnClickListener({
            mRightOnClickListener?.onRightImgClick()
        })
    }


    /**
     * 打卡软键盘
     */
    fun openKeyBord(mEditText: EditText, mContext: Context) {
        val imm = mContext.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        imm.showSoftInput(mEditText, InputMethodManager.RESULT_SHOWN)
        imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY)
    }

    /**
     * 关闭软键盘
     */
    fun closeKeyBord(mEditText: EditText, mContext: Context) {
        val imm = mContext.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        imm.hideSoftInputFromWindow(mEditText.windowToken, 0)
    }


    override fun onDestroy() {
        super.onDestroy()
        val application = MyApplication.getAplication()
        application?.removeActivity(this)
        compositeDisposable.clear()
    }

    fun showDialog() {
        showDialog?.show("loading...")
    }

    fun dissmissDialog() {
        showDialog?.dismiss()
    }

    /**
     * 监听返回图标
     */
    interface onRightClickListener {
        fun onRightImgClick()
    }

    private var mRightOnClickListener: onRightClickListener? = null

    fun setOnRightImgClickListener(listener: onRightClickListener) {
        this.mRightOnClickListener = listener
    }
}